New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@alterior/di

Package Overview
Dependencies
Maintainers
0
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@alterior/di

Flexible reflection-based dependency injection

  • 3.11.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
0
Weekly downloads
 
Created
Source

@alterior/di

Version

Alterior supports dependency injection using the same patterns as in Angular applications. @alterior/di is responsible for providing the underlying dependency injection mechanism.

Injectors created via this library are heirarchical. This is an important concept which is used throughout the Alterior framework. Injectors provide the dependencies specified when they are resolved and if no matching dependency is found, the request is forwarded to the parent injector.

import { ReflectiveInjector, Injectable } from '@alterior/di';

@Injectable()
class TestService {
    constructor(readonly date : Date) {
    }

    hello() {
        console.log(`Hello! The time is ${this.date.toString()}`);
    }
}

let injector = ReflectiveInjector.resolveAndCreate([
    { provide: 'Test', useValue: 123 },
    { provide: Date, useValue: new Date() },
    TestService
]);

console.log(injector.get('Test'));
console.log(injector.get(Date));
injector.get(TestService).hello();

Injectors

The Injector class is abstract. It has exactly one exposed method: get(token : any) : any. The library ships with ReflectiveInjector which uses Typescript's runtime type reflection (requires emitDecoratorMetadata) in order to determine which dependencies are requested by a constructor function. Because Typescript only emits type metadata for elements which are annotated with a decorator, you must decorate any class that participates in DI with the @Injectable() decorator. Since the mere presence of a decorator causes metadata to be emitted, you do not need @Injectable() if there is any other decorator already applied, but it is good practice to include it nonetheless.

Providers

An Injector is defined by a set of Providers. A Provider specifies an injection token to be provided as well as the value which should be injected when the framework encounters the injection token.

There are a number of Provider types:

  • Value: Specify an existing value that should be provided
    { provide: 'TOKEN', useValue: 123 }

  • Factory: Define a factory function that will be called in order to create the value that should be provided
    { provide: 'TOKEN', useFactory: () => 123 }

  • Existing: Define the provided value by specifying another injection token to resolve
    { provide: 'TOKEN', useExisting: 'ANOTHERTOKEN' }

  • Class: The injector will instantiate the given class and use the new instance as the provided value
    { provide: 'TOKEN', useClass: MyClass }

As a shortcut, you can pass a constructor function (class) without wrapping it in a useClass Provider object. Doing so with class ABC is the equivalent of specifying { provide: ABC, useClass: ABC }

Creating Injectors

let injector = ReflectiveInjector.resolveAndCreate([ /* providers */ ], parent?);

Use resolveAndCreate() to create a new injector. If you passed a parent injector to resolveAndCreate() then the tokens provided by the parent injector become available in the new injector (assuming they are not overridden).

Getting Values

Once you've obtained an Injector you can use Injector#get() to obtain the value for a particular injection token:

let instance = injector.get(MyClass);

If MyClass is not provided by the injector (or any of its parents) an exception will be thrown. You can change this behavior by passing a second parameter to get():

let number = injector.get('SomeNumber', 999);

Here number will be 999 if injector does not provide 'SomeNumber'. You can use this to avoid throwing an exception if the value is not present:

let instance = injector.get(MyClass, null);

Here instance will be null if injector does not provide MyClass. Note: You cannot pass undefined here as get() will act as if you did not pass the second parameter at all. Use null instead.

Keywords

FAQs

Package last updated on 15 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc